package net.lab1024.sa.admin.module.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.workflow.domain.entity.LeaveApply;

import net.lab1024.sa.admin.module.workflow.domain.leave.bo.LeaveApplyBo;
import net.lab1024.sa.admin.module.workflow.domain.vo.LeaveApplyVo;
import net.lab1024.sa.admin.module.workflow.service.LeaveApplyService;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartExcelUtil;
import net.lab1024.sa.base.module.support.repeatsubmit.annoation.RepeatSubmit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;

/**
 * 请假
 *
 * @author ruoyi
 * @date 2024-12-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/leaveapply")
@Slf4j
public class LeaveApplyController {

    private final LeaveApplyService leaveapplyService;

    /**
     * 查询请假列表
     */
//    @SaCheckPermission("system:leaveapply:list")
//    @GetMapping("/list")
//    public ResponseDTO<PageResult<LeaveapplyVo>> list(LeaveapplyBo bo) {
//        PageResult<LeaveapplyVo> pageResult = leaveapplyService.queryPageList(bo);
//        return ResponseDTO.ok(pageResult);
//    }

    /**
     * 导出请假列表
     */
    @SaCheckPermission("system:leaveapply:export")
    @PostMapping("/export")
    public void export(LeaveApplyBo bo, HttpServletResponse response) throws IOException {
        List<LeaveApplyVo> list = leaveapplyService.queryList(bo);
        //    ExcelUtil.exportExcel(list, "请假", LeaveapplyVo.class, response);
        SmartExcelUtil.exportExcel(response,"请假列表.xlsx","请假", LeaveApplyVo.class, list);

    }

    /**
     * 获取请假详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:leaveapply:query")
    @GetMapping("/{id}")
    public ResponseDTO<LeaveApply> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {

        LeaveApply leaveapply = leaveapplyService.queryById(id);
        return ResponseDTO.ok(leaveapply);
    }

    @SaCheckPermission("system:leaveapply:query")
    @GetMapping("/detail/{procInsId}")
    public ResponseDTO<LeaveApply> getInfoByProcInsId(@NotNull(message = "主键不能为空") @PathVariable String procInsId) {
        LeaveApply leaveapply = leaveapplyService.queryByProcInsId(procInsId);

        return ResponseDTO.ok(leaveapply);
    }

    @PostMapping("/preview2/{processKey}")
    @RepeatSubmit()
    public ResponseDTO<String> previewLeave2(@PathVariable(value = "processKey") String processKey ,  LeaveApplyBo bo) {

        return ResponseDTO.ok(leaveapplyService.previewLeave2(processKey, bo));
    }
}
